package com.jgc.stage02.同步工具类;

import java.util.Random;
import java.util.concurrent.Phaser;
import java.util.concurrent.TimeUnit;

/**
 * Created by Administrator on 2022/2/11.
 */
public class TestPhaser {
    static MyPhase phase = new MyPhase();

    public static void main(String[] args){
        // 参数婚礼的总人数为7
        phase.bulkRegister(7);
        // 5个参加婚礼的客人
        for (int i = 0;i<5;i++){
            new Thread(new Person("P"+i)).start();
        }
        // 新郎和新娘
        new Thread(new Person("新郎")).start();
        new Thread(new Person("新娘")).start();

    }
    /**
     * 参加婚礼的人，含新郎新娘
     */
    static class Person implements Runnable{

        private String name;

        public Person(String name) {
            this.name = name;
        }
        public  void arrived() {
            try {
                TimeUnit.MILLISECONDS.sleep(new Random().nextInt(1000));
                System.out.printf("%s is arrived! \n",name);
                phase.arriveAndAwaitAdvance();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        public  void eat() {
            try {
                TimeUnit.MILLISECONDS.sleep(new Random().nextInt(1000));
                System.out.printf("%s is eated! \n",name);
                phase.arriveAndAwaitAdvance();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        public  void leave() {
            try {
                TimeUnit.MILLISECONDS.sleep(new Random().nextInt(1000));
                System.out.printf("%s is leave! \n",name);
                phase.arriveAndAwaitAdvance();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        public  void xx() {
            try {
                if(name.equals("新郎")|| name.equals("新娘")) {
                    TimeUnit.MILLISECONDS.sleep(new Random().nextInt(1000));
                    System.out.printf("%s is xx! \n",name);
                    phase.arriveAndAwaitAdvance();
                }else {
                    // 客人不参与XX 到达这个阶段就直接撤销。
                    phase.arriveAndDeregister();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void run() {
            // 每个人需要依次执行到达，吃饭，离席，XX（客人就不要参加了）
            arrived();
            eat();
            leave();
            xx();
        }
    }


    static class MyPhase extends Phaser {

        @Override
        protected boolean onAdvance(int phase, int registeredParties) {
            switch (phase){
                case 0:{
                    System.out.println("所有人到齐了，人数为："+registeredParties);
                    return false;
                }
                case 1:{
                    System.out.println("所有人开始吃饭 !!人数为："+registeredParties);
                    return false;
                }
                case 2:{
                    System.out.println("所有人吃完了，离席!!!人数为："+registeredParties);
                    return false;
                }
                case 3:{
                    System.out.println("xx的人数为："+registeredParties);
                    return true;
                }
                default:{
                    return true;
                }
            }
        }
    }
}
